#include "ULUI_StatisticAllOut.h"
#include "UL_Global.h"
#include "ULGfx_History.h"
#include <algorithm>

#define STATISTIC_ALL_OUT_START_X	100
#define STATISTIC_ALL_OUT_START_Y	100

ULUI_StatisticAllOut ULUI_StatisticAllOut::statisticAllOut;

enum HistoryLookupIDs{
	EXIT	
};	

static char *menuTitles[] = {
	"返回",
};

#define FIXED_GAP	20

void ULUI_StatisticAllOut::Init()
{
	int i;
	ULUI_Menu menu;
	char buf[8] = {0};

	AttachEventHandler(this);

	menu.SetFont(UL_Global::UnionLottoFont());
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	//Find menu's X is after tip
	menu.SetTitle("                                ");
	menu.GetTitleSize(tipWidth, tipHeight);
	menus.SetFirstMenuXY(VSDL_Point(STATISTIC_ALL_OUT_START_X + tipWidth, STATISTIC_ALL_OUT_START_Y + tipHeight));

	//Setup menus for blue numbers
	numbers.SetVertical(false);
	menu.ShowBoarder(true);
	for (i = 0; i < UL_RED_BALLS_COUNT; i++){
		sprintf(buf, "红球%d", i + 1);
		menu.SetID(i);
		menu.SetTitle(buf);
		numbers.AddMenu(menu);
	}

	menu.SetID(i);
	menu.SetTitle("蓝球");
	numbers.AddMenu(menu);

	numbers.SetFirstMenuXY(VSDL_Point(STATISTIC_ALL_OUT_START_X + tipWidth, STATISTIC_ALL_OUT_START_Y - 2));

	selectedIndex = -1;
}

void ULUI_StatisticAllOut::CleanUp()
{
	menus.Reset();
	numbers.Reset();
}

void ULUI_StatisticAllOut::Pause()
{
}

void ULUI_StatisticAllOut::Resume()
{
	selectedMenu = -1;
}

void ULUI_StatisticAllOut::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case EXIT:
			game->PopState();
			break;
	}
}

#define BALLS_PRE_ROW	8	

void ULUI_StatisticAllOut::Draw(VSDL_GameEngine *game)
{
	VSDL_RGB rgbNormal(100, 100, 100);
	VSDL_RGB rgbSelected(250, 100, 100);
	VSDL_RGB rgb;
	char *str;
	VSDL_Point currentXY(STATISTIC_ALL_OUT_START_X, STATISTIC_ALL_OUT_START_Y);
	SDL_Surface *destSurf = game->GetSurface();
	ULG_History *ulgHistory = ULG_History::Instance();;
	int historyY = STATISTIC_ALL_OUT_START_Y + (EXIT + 3) * tipHeight;
	char buf[128];


	ulgHistory->SetStartXY(VSDL_Point(STATISTIC_ALL_OUT_START_X, historyY));

	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(destSurf);
	numbers.Show(destSurf);
	//Draw history data background
	SPG_RectFilled(destSurf, STATISTIC_ALL_OUT_START_X, historyY, destSurf->w, destSurf->h, rgbNormal.GetRGB32(destSurf));
	//Show tips
	UL_Global::UnionLottoShowTip("选择要查看的球", currentXY);
	//Draw spliting line
	historyY--;
	SPG_Line(destSurf, currentXY.x, historyY, destSurf->w, historyY, rgbNormal.GetRGB32(destSurf));
	//Draw statistic
	if (selectedIndex < 0 || selectedIndex > UL_RED_BALLS_COUNT){
		return ;
	}

	ULC_Statistic *ulcStat = UL_Global::UnionLottoTotalStatistic();
	ULC_PositionInfo *info;
	ULG_BallStatistic *ulgBallStat;
	list<ULC_BallFrequency>::iterator iter;
	bool red;
	int count = 0;

	if (selectedIndex < UL_RED_BALLS_COUNT){
		//Red
		red = true;
		ulgBallStat = UL_Global::UnionLottoBallStatistic(true);

		info = ulcStat->GetOutRedInfo(selectedIndex);

		if (info == NULL){
			return ;
		}
	}else{
		//Blue
		red = false;
		ulgBallStat = UL_Global::UnionLottoBallStatistic(false);

		info = ulcStat->GetBlueInfo();

		if (info == NULL){
			return ;
		}
	}

	currentXY.y = historyY;

	for (iter = info->numberOrder.begin(); iter != info->numberOrder.end(); iter++){
		ulgBallStat->Show(destSurf, *iter, red, currentXY);
		count++;
		currentXY.x += ulgBallStat->GetWidth() + FIXED_GAP;
		if (count == BALLS_PRE_ROW){
			currentXY.x = STATISTIC_ALL_OUT_START_X; 
			currentXY.y += ulgBallStat->GetHeight(); 
			count = 0;
		}
	}

}

ULUI_StatisticAllOut *ULUI_StatisticAllOut::Instance()
{
	return &statisticAllOut;
}


void __HANDLE_EVENT ULUI_StatisticAllOut::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	//Basic
	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}

}

void __HANDLE_EVENT ULUI_StatisticAllOut::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
	numbers.UpdateMenuList(absX, absY);
}

void __HANDLE_EVENT ULUI_StatisticAllOut::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}

	for (i = 0; i < UL_RED_BALLS_COUNT + 1; i++){
		ULUI_Menu &menu = numbers.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedIndex = i;
		}
	}
}

void __HANDLE_EVENT ULUI_StatisticAllOut::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}
